#include "Player.h"

using namespace std;

Player::Player()
{
	Pokemon firstPokemon = Pokemon(PokemonLoader::GetInstance()->GetPokemonData(3*(rand() % 3) + 1), STARTER_LEVEL);
	firstPokemon.SetCurrentXP( STARTER_LEVEL * STARTER_LEVEL * STARTER_LEVEL );
	
	m_AllPokemons.push_back(firstPokemon);
	m_Party.push_back(0);
}

Player::Player(const vector<Pokemon>& _source)
{
	for(int i = 0; i < _source.size(); i++)
	{
		m_AllPokemons.push_back(_source[i]);
	}

	int count = 0;
	while(count < MAX_POKEMON_IN_PARTY && count < m_AllPokemons.size())
	{
		m_Party.push_back(count);
		count++;
	}
}

Player::Player(const Player& _source)
{
	for(int i = 0; i < _source.m_AllPokemons.size(); i++)
	{
		m_AllPokemons.push_back(_source.m_AllPokemons[i]);
	}

	for(int i = 0; i < _source.m_Party.size(); i++)
	{
		m_Party.push_back(_source.m_Party[i]);
	}
}

Player::~Player()
{

}

int Player::GetPokemonLeftInParty()
{
	int count = 0;

	for(int i = 0; i < m_Party.size(); i++)
	{
		if(!GetPokemonInParty(i).IsDead())
			count++;
	}

	return count;
}

void Player::DisplayParty() const
{
	for(int i = 0; i < m_Party.size(); i++)
	{
		cout << "#" << i+1 << "\t" << m_AllPokemons[m_Party[i]].GetCurrentHP() << " / " << m_AllPokemons[m_Party[i]].GetMaxHP() << "\t" << m_AllPokemons[m_Party[i]].GetName() << endl;
	}
}

void Player::DisplayAllPokemons() const
{
	for(int i = 0; i < m_AllPokemons.size(); i++)
	{
		cout << m_AllPokemons[i] << endl;
	}
}

void Player::InitAllPokemons()
{
	for(int i = m_AllPokemons.size()-1; i >= 0; i--)
	{
		m_AllPokemons[i].InitPokemon();
	}
}

void Player::AddPokemon(Pokemon& _newPokemon)
{
	m_AllPokemons.push_back(_newPokemon);

	if(m_Party.size() < MAX_POKEMON_IN_PARTY)
		m_Party.push_back( m_AllPokemons.size()-1 );
}

Pokemon* Player::GetPokemonInList(const Pokemon* const _pokemon)
{
	for(int i = m_AllPokemons.size() - 1; i >= 0; i--)
	{
		if(m_AllPokemons[i] == *_pokemon)
			return &(m_AllPokemons[i]);
	}

	return 0;
}

void Player::PickStarterPokemon()
{
	cout << "Pick your first pokemon" << endl << endl;
	cout << "1. Bulbasaur" << endl;
	cout << "2. Charmander" << endl;
	cout << "3. Squirtle" << endl << endl;

	int entry;

	do
	{
		cin >> entry;
	}while(entry != 1 && entry != 2 && entry != 3);

	Pokemon firstPokemon = Pokemon(PokemonLoader::GetInstance()->GetPokemonData(1 + 3*(entry-1)), STARTER_LEVEL);
	firstPokemon.SetCurrentXP( STARTER_LEVEL * STARTER_LEVEL * STARTER_LEVEL );
	m_AllPokemons.push_back(firstPokemon);
	m_Party.push_back(0);	
}

bool Player::IsAlive() const
{
	for(int i = m_AllPokemons.size() - 1; i >= 0; i--)
	{
		if(!m_AllPokemons[i].IsDead())
			return true;
	}

	return false;
}